#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_register.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->window = new Register;
    connect(window,&Register::goback,this,[=](){
        window->close();
        this->show();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::checkLogin(const QString& username, const QString& password, vector<user> Users){
    for (const auto& usr : Users) {
        if (usr.user_id == username.toStdString() && user::get_hashcode(password.toStdString()) == usr.code) {
            return true;
        }
    }
    return false;
}

void MainWindow::on_pushButton_clicked()
{
    //获取lineedit
    QString username = ui->lineEdit_2->text();
    QString password = ui->lineEdit->text();

    vector<user> Users = loadUsers();
    // 检查用户名和密码是否匹配
    if (checkLogin(username, password, Users)) {
        QMessageBox::information(this, "Login", "Login successful!");
    } else {
        QMessageBox::warning(this, "Login", "Invalid username or password.");
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    this->hide();
    window->show();
}
